package org.javaboy.demo.controller;

import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.javaboy.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 *
 */
@Controller
public class UserController {

    //    @RequestMapping(value = "/user",method = RequestMethod.GET)
    @GetMapping("/user")
    @ResponseBody
    public User getUser() {
        User user = new User();
        user.setId(1);
        user.setUsername("张三");
        user.setAddress("深圳");
        return user;
    }

    @PostMapping("/user")
    @ResponseBody
    public void addUser(@RequestBody User user) {
        System.out.println("user = " + user);
    }

    /**
     * http://localhost:8080/j/user?username=xxx
     * http://localhost:8080/j/user/zhangsan
     *
     * @param request
     */
    public void test1(HttpServletRequest request) throws IOException {
        //解析输入流，就可以拿到请求体中的数据
//        ServletInputStream inputStream = request.getInputStream();
//        ObjectMapper om = new ObjectMapper();
//        User user = om.readValue(inputStream, User.class);

    }
}
